/**
 * 
 */
package net.toocruel.iqismart.entity.rbac;

import net.toocruel.iqismart.entity.base.BaseEntity;
import springfox.documentation.annotations.ApiIgnore;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 角色信息
 * 
 * @author toocruel
 *
 */
@Entity
public class Role extends BaseEntity {

	private static final long serialVersionUID = 88707052810990594L;
	/**
	 * 角色名称
	 */
	@Column(length = 20, nullable = false)
	private String name;
	/**
	 * 角色拥有权限的资源集合
	 */
	@ManyToMany(cascade = CascadeType.DETACH,fetch = FetchType.EAGER)
	@JoinTable(name = "role_resource_relation",joinColumns = @JoinColumn(name = "roleId"),inverseJoinColumns = @JoinColumn(name = "resourceId"))
	private Set<Resource> resources  = new HashSet<>();

	@Transient
	private Long[] resourceIds;

	public Role() {
	}

	public Role(long id, String name) {
		this.id = id;
		this.name = name;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	@ApiIgnore
	public Set<Resource> getResources() {
		return resources;
	}

	public void setResources(Set<Resource> resources) {
		this.resources = resources;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Role role = (Role) o;
		return id.equals(role.id);
	}

	@Override
	public int hashCode() {
		return Objects.hash(id);
	}

	public Long[] getResourceIds() {
		return resourceIds;
	}

	public void setResourceIds(Long[] resourceIds) {
		this.resourceIds = resourceIds;
	}
}
